package com.example.room_type.controller;

import com.example.core.annotation.HasPerm;
import com.example.model.response.Result;
import com.example.model.room_type.RoomType;
import com.example.room_type.service.RoomTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roomType")
public class
RoomTypeController {

    @Autowired
    private RoomTypeService roomTypeService;

    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Long id) {
        return Result.success(roomTypeService.get(id));
    }

    @HasPerm("management:roomType:list")
    @GetMapping("/list")
    public Result list(RoomType roomType) {
        return Result.success(roomTypeService.list(roomType));
    }

    @HasPerm("management:roomType:list")
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize,RoomType roomType) {
        PageHelper.startPage(pageNum, pageSize);
        List<RoomType> list = roomTypeService.list(roomType);
        PageInfo<RoomType> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @HasPerm("management:roomType:insert")
    @PostMapping
    public Result insert(@Valid @RequestBody RoomType roomType) {
        return Result.success(roomTypeService.insert(roomType) == 1 ? "新增成功" : "新增失败");
    }

    @HasPerm("management:roomType:update")
    @PutMapping
    public Result update(@Valid @RequestBody RoomType roomType) {
        return Result.success(roomTypeService.update(roomType) == 1 ? "更新成功" : "更新失败");
    }

    @HasPerm("management:roomType:delete")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(roomTypeService.delete(id) == 1 ? "删除成功" : "删除失败");
    }

}
